package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.domain.FamilyMemberElder;
import com.zzyl.nursing.dto.FamilyMemberElderDto;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.mapper.FamilyMemberElderMapper;
import com.zzyl.nursing.service.IElderService;
import com.zzyl.nursing.service.IFamilyMemberElderService;
import com.zzyl.nursing.vo.FamilyMemberElderVo;
import com.zzyl.nursing.vo.MemberElderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

import static com.zzyl.common.utils.PageUtils.startPage;

/**
 * 客户老人关联Service业务层处理
 * 
 */
@Service
public class FamilyMemberElderServiceImpl extends ServiceImpl<FamilyMemberElderMapper, FamilyMemberElder> implements IFamilyMemberElderService
{
    @Autowired
    private FamilyMemberElderMapper familyMemberElderMapper;
    @Autowired
    private ElderMapper elderMapper;

    /**
     * 查询客户老人关联
     * 
     * @param id 客户老人关联主键
     * @return 客户老人关联
     */
    @Override
    public FamilyMemberElder selectFamilyMemberElderById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询客户老人关联列表
     * 
     * @param familyMemberElder 客户老人关联
     * @return 客户老人关联
     */
    @Override
    public List<FamilyMemberElder> selectFamilyMemberElderList(FamilyMemberElder familyMemberElder)
    {
        return familyMemberElderMapper.selectFamilyMemberElderList(familyMemberElder);
    }

    /**
     * 新增客户老人关联
     * 
     * @param familyMemberElder 客户老人关联
     * @return 结果
     */
    @Override
    public int insertFamilyMemberElder(FamilyMemberElder familyMemberElder)
    {
        return save(familyMemberElder) ? 1 : 0;
    }

    /**
     * 修改客户老人关联
     * 
     * @param familyMemberElder 客户老人关联
     * @return 结果
     */
    @Override
    public int updateFamilyMemberElder(FamilyMemberElder familyMemberElder)
    {
        return updateById(familyMemberElder) ? 1 : 0;
    }

    /**
     * 批量删除客户老人关联
     * 
     * @param ids 需要删除的客户老人关联主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberElderByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除客户老人关联信息
     * 
     * @param id 客户老人关联主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberElderById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Autowired
    private IElderService elderService;

    /**
     * 绑定家人
     *
     * @param familyMemberElderDto 家人信息
     * @return  0 失败 1 成功
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int bindingFamilyMember(FamilyMemberElderDto familyMemberElderDto) {
        // 1. 根据老人身份证号查询老人
        Elder elder = elderService.lambdaQuery().eq(Elder::getIdCardNo, familyMemberElderDto.getIdCard()).one();
        if(elder==null){
            throw new BaseException("老人不存在");
        }

        //获取登录用户id
        Long userId = UserThreadLocal.getUserId();

        // 2. 判断是否已经绑定过此家人
        Long count = this.lambdaQuery().eq(FamilyMemberElder::getElderId, elder.getId())
                .eq(FamilyMemberElder::getFamilyMemberId, userId).count();
        if(count>0) {
            throw new BaseException("已绑定过此家人，不要重复绑定");
        }

        // 3. 封装关系数据
        FamilyMemberElder familyMemberElder = new FamilyMemberElder();
        familyMemberElder.setFamilyMemberId(userId);
        familyMemberElder.setElderId(elder.getId());
        familyMemberElder.setRemark(familyMemberElderDto.getRemark());

        // 4.保存关系数据
        boolean result = this.save(familyMemberElder);

        return result ? 1 : 0;
    }

    /**
     * 查询家人列表
     *
     * @return 家人列表
     */
    @Override
    public List<FamilyMemberElderVo> getMyFamilyMember() {
        Long userId = UserThreadLocal.getUserId();
        return familyMemberElderMapper.getMyFamilyMember(userId);
    }

    /**
     * 分页查询已绑定家人列表
     *
     * @return 已绑定家人列表
     */
    @Override
    public List<MemberElderVo> listByPage() {
        startPage();
        Long memberId = UserThreadLocal.get();
        return familyMemberElderMapper.listByPage(memberId);
    }

    /**
     * 解绑家人
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteFamilyMember(Long id) {
        //1. 判断关系是否存在
        FamilyMemberElder familyMemberElder = this.lambdaQuery().eq(FamilyMemberElder::getId, id).one();
        if(familyMemberElder==null) {
            throw new BaseException("不存在该绑定的家人");
        }

        //获取登录用户id
        Long userId = UserThreadLocal.getUserId();
        // 2. 判断是否我自己绑定家人
        Long count = this.lambdaQuery()
                .eq(FamilyMemberElder::getElderId, familyMemberElder.getElderId())
                .eq(FamilyMemberElder::getFamilyMemberId, userId)
                .count();
        if(count<=0) {
            throw new BaseException("没有绑定过此家人，无法删除");
        }

        //3. 删除关系数据
        boolean result = this.removeById(id);
        return result ? 1: 0;
    }
}
